﻿using Dapper;
using InventoryIssue.API.Read.Application.Command.Reportloss;
using InventoryIssue.Dtos;
using InventoryIssue.ErrorCode;
using InventoryIssue.Infrastructrue.Implement;
using MediatR;
using System.Text;

namespace InventoryIssue.API.Read.Application.Handler.Reportloss
{
    public class GetReportlossHandler : IRequestHandler<GetReportlossCommand, APIPaging<ReportlossDto>>
    {
        private readonly DatabaseContext db;

        public GetReportlossHandler(DatabaseContext db)
        {
            this.db = db;
        }
        public async Task<APIPaging<ReportlossDto>> Handle(GetReportlossCommand request, CancellationToken cancellationToken)
        {
            APIPaging<ReportlossDto> result = new APIPaging<ReportlossDto>();
            StringBuilder sql = new StringBuilder();
            StringBuilder countSql = new StringBuilder();
            sql.Append(@"
                    SELECT
	                    a.Note,
	                    a.OrderNumber,
	                    a.ReportlossId,
	                    a.reportlossType,
	                    a.State,
	                    a.OrderCreator,
	                    a.OrderCreatorTime,
	                    b.quantity,
	                    b.Id,
	                    c.ProductName,
	                    c.ProductCode,
	                    c.Specifications,
	                    d.UnitName,
	                    e.DepotName,
	                    f.LocationName 
                    FROM
	                    reportlossmodel a
	                    JOIN reportlossproductmodel b ON a.ReportlossId = b.ReportlossId
	                    JOIN productmodel c ON b.ProductId = c.ProductId
	                    JOIN unitmodel d ON c.UnitId = d.UnitId
	                    JOIN depotmodel e ON c.DepotId = e.DepotId
	                    JOIN locationmodel f ON c.LocationId = f.LocationId 
                    WHERE
	                    a.IsDel = 0 
	                    AND b.IsDel = 0 
	                    AND c.IsDel = 0
	                    AND d.IsDel = 0
	                    AND e.IsDel = 0
	                    AND f.IsDel = 0
                ");
            countSql.Append(@"SELECT
	                            COUNT(a.Id)
                            FROM
	                            reportlossmodel a
	                            JOIN reportlossproductmodel b ON a.ReportlossId = b.ReportlossId
	                            JOIN productmodel c ON b.ProductId = c.ProductId
	                            JOIN unitmodel d ON c.UnitId = d.UnitId
	                            JOIN depotmodel e ON c.DepotId = e.DepotId
	                            JOIN locationmodel f ON c.LocationId = f.LocationId 
                            WHERE
	                            a.IsDel = 0 
	                            AND b.IsDel = 0 
	                            AND c.IsDel = 0
	                            AND d.IsDel = 0
	                            AND e.IsDel = 0
	                            AND f.IsDel = 0");
            if (!string.IsNullOrEmpty(request.ReportlossId))
            {
                sql.Append(" AND a.ReportlossId LIKE @ReportlossId");
                countSql.Append(" AND a.ReportlossId LIKE @ReportlossId");
            }
            if (!string.IsNullOrEmpty(request.ReportlossType))
            {
                sql.Append(" AND a.ReportlossType= @ReportlossType");
                countSql.Append(" AND a.ReportlossType= @ReportlossType");
            }
            if (request.State.HasValue)
            {
                sql.Append(" AND a.State = @State");
                countSql.Append(" AND a.State = @State");
            }
            sql.Append(" ORDER BY a.OrderCreatorTime DESC LIMIT @PageSize OFFSET @offset");
            var parameters = new
            {
                ReportlossId = request.ReportlossId,
                ReportlossType = request.ReportlossType,
                State = request.State,
                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };
            var pageDatas = await db.DapperConnection.QueryAsync<ReportlossDto>(sql.ToString(), parameters);
            int totalCount = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);
            int totalPage = (int)Math.Ceiling(totalCount / (double)request.PageSize);
            result.Message = "显示成功";
            result.TotalPage = totalPage;
            result.TotalCount = totalCount;
            result.PageDatas = pageDatas.ToList();
            return result;
        }
    }
}
